/**
@file		commhead.h
@brief	轻型公共库,通用头文件.(editor tab=4)

@author		chenht
@date		2008/08/11
@history		chenht edit,2010/05/15

*/

#ifndef	__COMMHEAD_H__
#define	__COMMHEAD_H__

/* If anything changes in the following list of #includes, must change
   acsite.m4 also, for configure's tests. */

#include	<sys/types.h>	/* basic system data types */
#include	<sys/socket.h>	/* basic socket definitions */
#include	<sys/time.h>	/* timeval{} for select() */
#include <sys/timeb.h>  /* for ftime() */

#include	<time.h>		/* timespec{} for pselect() */
#include	<netinet/in.h>	/* sockaddr_in{} and other Internet defns */
#include	<arpa/inet.h>	/* inet(3) functions */
#include	<errno.h>
#include	<fcntl.h>		/* for nonblocking */
#include	<netdb.h>
#include	<signal.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<sys/stat.h>	/* for S_xxx file mode constants */
#include	<sys/uio.h>		/* for iovec{} and readv/writev */
#include	<unistd.h>
#include	<sys/wait.h>
#include	<sys/un.h>		/* for Unix domain sockets */
#include	<sys/select.h>	/* for convenience */
#include	<poll.h>		/* for convenience */
#include	<strings.h>		/* for convenience */

/* Three headers are normally needed for socket/file ioctl's:
 * <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>.
 */
#include	<sys/ioctl.h>
#include	<pthread.h>

#include <string>
#include <vector>
#include <map>
#include <set>

using std::string;
using std::map;

#define COMM_LIB_VERSION		"comm_lib_1.0"

#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif

//这里定义server端和api都要用到的宏和结构
enum CommErrCode
{
    ERR_FAIL		= -1,   /*失败、条件(假)*/
    ERR_OK			= 0,    /*成功、条件(真)*/

    ERR_SQL		= 100,      /*sql执行错误*/
};


/** ***************
	\brief 下面是一般数值操作函数整理
*/

/** 
	\brief 取两个值大的
	\param a,b: 数值.
*/
#define COMM_MAX(a, b) ((a > b) ? a : b)

/** 
	\brief 取两个值小的
	\param a,b: 数值.
*/

#define COMM_MIN(a, b) ((a < b) ? a : b)

#define COMM_NUMBER_ALLIGN(num, unit)			\
	(((num) % (unit) == 0) ?				\
	(num) :									\
	((num) + (unit) - (num) % (unit)))


/** 
	\brief Increase the value of signed integer, make sure that the integer does not overflow.
	\param n: Signed integer.
*/
#define COMM_INCREASE_INT(n) n = (n >= 0x7FFFFFFF) ? 0 : n + 1

/** 
	\brief Increase the value of unsigned integer, make sure that the integer does not overflow.
	\param n: Unsiged integer.
*/
#define COMM_INCREASE_UINT(n) n = (n >= 0xFFFFFFFF) ? 0 : n + 1

/** 
	\brief Increase the value of signed integer, make sure that the integer does not overflow.
	\param n: Signed integer.
	\param m: The number to be plus.
*/
#define COMM_PLUS_INT(n, m)	\
{							\
	if(0x7FFFFFFF - n) > m)	\
	{						\
		n = n + m;			\
	}						\
}

/** 
	\brief Increase the value of unsigned integer, make sure that the integer does not overflow.
	\param n: Unsiged integer.
	\param m: The number to be plus.
*/
#define COMM_PLUS_UINT(n, m)					\
{											\
	if(0xFFFFFFFF - n > m)					\
	{										\
		n = n + m;							\
	}										\
}



/** ***************
	\brief 下面是安全操作函数整理
*/

/** 
	\brief delete an object in safe way
	\param obj: object pointer.
*/
#define SAFE_DELETE(obj)		\
{								\
	if(obj != NULL)				\
	{							\
		delete obj;				\
		obj = NULL;				\
	}							\
}

/** 
	\brief free a memory block in safe way
	\param p: memory block pointer.
*/
#define SAFE_FREE(p)			\
{								\
	if(p != NULL)				\
	{							\
		free(p);				\
		p = NULL;				\
	}							\
}

/** 
	\brief close file in safe way
	\param p: file handle
*/
#define SAFE_FCLOSE(pfile)	\
{								\
	if(pfile != NULL)			\
	{							\
		fclose(pfile);			\
		pfile = NULL;			\
	}							\
}

#define SAFE_MEMCPY(dest, src, len)	\
{									\
	if(len > 0)						\
	{								\
		memcpy(dest, src, len);		\
	}								\
}


/** ***************
	\brief 以毫秒为单位的sleep
	\param msec: The sleep time, in millisecond.
*/
inline void Sleep(int msec)
{
	struct timeval tval;
	if(msec < 1)
		msec = 1;
	tval.tv_sec = msec/1000;
	tval.tv_usec = (msec%1000) * 1000;
	select(0,(fd_set*)0,(fd_set*)0,(fd_set*)0,&tval);
		
}

#endif	//__COMMHEAD_H__

